package com.scoy.honeymei.activity.home;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.bean.FilmDateBean;
import com.scoy.honeymei.databinding.ActivityFilmListBinding;
import com.scoy.honeymei.dialog.ShowFullDialog;
import com.scoy.honeymei.fragment.home.FilmFragment;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FilmListActivity$
 * @Description: 电影
 * @Author: GaoZhan
 * @CreateDate: 2019/9/4/0004$ 上午 8:59$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/4/0004$ 上午 8:59$
 * @UpdateRemark: 更新内容
 */
public class FilmListActivity extends BaseActivity {


    private FilmListActivity mContext;
    private int type;
    private int mid;
    private ActivityFilmListBinding binding;
    private FilmBean filmData;
    private int cinemaId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFilmListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mContext = FilmListActivity.this;
        type = getIntent().getIntExtra("pos", 0);
        mid = getIntent().getIntExtra("mid", 0);
        cinemaId = getIntent().getIntExtra("cinemaId", 0);
        filmData = (FilmBean) getIntent().getParcelableExtra("filmData");

        initNormal();

        httpDate();

    }

    @Override
    public void initNormal() {
        binding.layTop.titleTv.setText(R.string.film);

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        binding.layTop.titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        initClick();
        initView();

    }

    private void initView() {

        String imagetop = filmData.getImage();
        if (imagetop != null && !"".equals(imagetop)) {
            binding.filmImgIv.setVisibility(View.VISIBLE);
            binding.jzVideo.setVisibility(View.GONE);
            GlideImgUtil.glidePicNo(mContext, imagetop, binding.filmImgIv);
            binding.filmImgIv.setOnClickListener(v -> {
                ShowFullDialog.newInstance(new ArrayList<String>() {{
                    add(imagetop);
                }}).show(getSupportFragmentManager(), 0 + "");
            });

        } else {
            binding.filmImgIv.setVisibility(View.GONE);
            binding.jzVideo.setVisibility(View.VISIBLE);
            binding.jzVideo.setUp(filmData.getVideo(), filmData.getTitle());
            binding.jzVideo.thumbImageView.setImageResource(R.mipmap.ic_hm_logo);
        }
        binding.filmNameTv.setText(filmData.getTitle());

    }

    private void initTab(List<FilmDateBean> data) {

        String[] mTitles = new String[data.size()];
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTitles[i] = TimeUtil.getLongToDatePHP(data.get(i).getTime(), "MM-dd");
            fragments.add(FilmFragment.newInstance(i, data.get(i).getTime(), mid, cinemaId));
        }
        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragments, mTitles);
        binding.famTab.setupWithViewPager(binding.famVp, false);
        binding.famVp.setAdapter(tabFragmentAdapter);
        binding.famVp.setCurrentItem(type);

    }

    private void initClick() {

        binding.layTop.backIv.setOnClickListener(v -> onBackPressed());

    }


    private void httpDate() {
        HttpParams params = new HttpParams();
        params.put("cinema_id", cinemaId);
        HpGo.newInstance().HttpGo(this, MyUrl.FILM_DATE, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<FilmDateBean> data = new Gson().fromJson(o, new TypeToken<List<FilmDateBean>>() {
                }.getType());
                if (data != null && data.size() > 0) {
                    initTab(data);
                }
            }
        });

    }

}
